/**
 * transform44.js
 * 
 * Implements the Transform44 class which manages a matrix44 created from
 * position, rotation and scale.
 * 
 * @author A.Weissflog
 * 
 * (C) 2010 A.Weissflog
 */

/**
 * The default constructor.
 */
function Transform44()
{
	this.position      = Float4.FromValues(0.0, 0.0, 0.0, 1.0);
	this.rotation      = new Quaternion();
	this.scale         = Float4.FromValues(1.0, 1.0, 1.0, 0.0);
	this.rotationPivot = Float4.FromValues(0.0, 0.0, 0.0, 1.0);
	this.scalePivot    = Float4.FromValues(0.0, 0.0, 0.0, 1.0);
	this.offset        = new Matrix44();
	this.matrix        = new Matrix44();
	this.isDirty       = false;
	this.offsetValid   = false;	
}

/**
 * Set the position, w component must be 1!
 * 
 * @param {Float4} pos
 */
Transform44.prototype.SetPosition = function(pos)
{
	this.position = pos;
	this.isDirty = true;
}

/**
 * Set the rotation as quaternion.
 * 
 * @param {Quaternion} rot
 */
Transform44.prototype.SetRotation = function(rot)
{
	this.rotation = rot;
	this.isDirty = true;
}

/**
 * Set the scaling, w component must be 0!
 * 
 * @param {Float4} s
 */
Transform44.prototype.SetScale = function(s)
{
	this.scale = s;
	this.isDirty = true;
}

/**
 * Set the rotation pivot, w component must be 1!
 * 
 * @param {Float4} p
 */
Transform44.prototype.SetRotationPivot = function(p)
{
	this.rotatePivot = p
	this.isDirty = true;
}

/**
 * Set the scale pivot, w component must be 1!
 * 
 * @param {Float4} p
 */
Transform44.prototype.SetScalePivot = function(p)
{
	this.scalePivot = p;
	this.isDirty = true;
}

/**
 * Set optional offset matrix.
 * 
 * @param {Matrix44} o
 */
Transform44.prototype.SetOffset = function(o)
{
	this.offset = o;
	this.isDirty = true;
	this.offsetValid = true;
}

/**
 * Get the resulting matrix.
 * 
 * @return {Matrix44}
 */
Transform44.prototype.GetMatrix = function()
{
	if (this.isDirty)
	{
		var identQuat = new Quaternion;
		this.matrix = Matrix44.Transformation(this.scalePivot, identQuat, this.scale, this.rotationPivot, this.rotation, this.position);
		if (this.offsetValid)
		{
			this.matrix.Multiply(this.offset);	
		}
		this.isDirty = false;
	}
	return this.matrix;
}

/**
 * Return true if the object is dirty.
 * 
 * @return {bool}
 */
Transform44.prototype.IsDirty = function()
{
	return this.isDirty;
}
